package com.fingard.diagnostics;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.text.StringHelper;

/**只记录固定个数的消息，循环擦写，如限制10条，则从1写到10，然后循环覆盖1到10*/
public class LimitMsg {

	private HashMap<String, String[]> mpMsg;
	private HashMap<String, Integer> mpIdx;//记录上次信息索引
	
	public LimitMsg(){
		mpMsg = new HashMap<String, String[]>();
		mpIdx = new HashMap<String, Integer>();
	}
	
	public ArrayList<String> getLimitDetail(String pKey){
		ArrayList<String> valueList = new ArrayList<String>();
		if(mpMsg.containsKey(pKey)){
			int idx = mpIdx.get(pKey);
			String[] msgs = mpMsg.get(pKey);
			for(int i=0;i<msgs.length;i++){
				
				int getidx = (idx+i)%msgs.length;
				
				if(StringHelper.hasAnyChar(msgs[getidx])){
					valueList.add(msgs[getidx]);
				}
			}
		}
		return valueList;
	}
	
	public ArrayList<String> getLimitKey(){
		ArrayList<String> keyList = new ArrayList<String>();
		Iterator<String> it = mpMsg.keySet().iterator();
		while (it.hasNext()) {
			keyList.add(it.next());
		}
		return keyList;
	}
	
	public void exception(String logCode, Throwable e, String... p_values){//异常等级8
		StringBuilder sbMsg = new StringBuilder();
		
		if(FGBiz.isRunDebugMode){
			String timeFrmt = Format.DateTimeSSSFormat.format(new Date());
			sbMsg.append(timeFrmt+ "\t"+logCode+"\t发生异常");	
		}
		
		if(p_values != null){
			for (int i = 0; i < p_values.length; i++) {
				sbMsg.append("\t");
	            sbMsg.append(p_values[i]);
	        }
			sbMsg.append("\r\n");
		}
		
		if(FGBiz.isRunDebugMode){
			System.out.println(sbMsg.toString());
		}
		e.printStackTrace();
		sbMsg.append(LogHelper.getStackTrace(e));
		add(logCode+"_Exception", 8, sbMsg.toString());
	}
	
	public void error(String logCode, String msg){//错误等级5
		add(logCode+"_Error", 5, msg);
	}
	
	public void warning(String logCode, String msg){//错误等级4
		add(logCode+"_Warning", 4, msg);
	}
	
	public void add(String logCode, String msg){//默认等级1
		add(logCode, 1, msg);
	}
	
	/**不输出到控制台*/
	public void addNoConsole(String logCode, String msg){
		add(logCode, 3, msg);
	}
	
	/**同时输出到控制台*/
	public void addConsole(String logCode, String msg){//等级2
		add(logCode, 2, msg);
	}
	
	private void add(String logCode,int pLevel, String msg){
		try{
			String timeFrmt = Format.DateTimeSSSFormat.format(new Date());
			if(pLevel == 2 || pLevel == 4 || pLevel == 5){//console warning error
				System.out.println(timeFrmt+ "\t"+logCode+"\t"+ msg);
			}else if(FGBiz.isRunDebugMode && pLevel == 1){//debugMode
				System.out.println(timeFrmt+ "\t"+logCode+"\t"+ msg);
			}
			String[] array;
			int idx;
			if(mpMsg.containsKey(logCode)){
				array = mpMsg.get(logCode);
				idx = mpIdx.get(logCode);
			}else{
				array = new String[10];
				idx = 0;
			}
			array[idx] = timeFrmt + "\t" + msg;
			idx = (idx+1)%array.length;
			
			mpMsg.put(logCode, array);
			mpIdx.put(logCode, idx);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
